/*
Date:20210530 17:34PM
key:在层序遍历循环里再加一个循环，循环次数位q.size()次，非常巧妙
 */
class Solution {
public:
    vector<vector<int>> levelOrder(TreeNode* root) {
        vector<vector<int>>v1;
        if(root==NULL){return v1;}
        queue<TreeNode*>q;
        q.push(root);
        //cout<<q.front()->val;
        while(!q.empty())
        {
            vector<int>v;
            int i=q.size();
            while(i--)
            {
                v.push_back(q.front()->val);
                if(q.front()->left!=NULL)
                {
                    q.push(q.front()->left);
                }
                if(q.front()->right!=NULL)
                {
                    q.push(q.front()->right);
                }
                
                q.pop();
            }
            v1.push_back(v);
        }
        return v1;
    }
};